#include<bits/types.h>
#include<unistd.h>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<exception>
#include<string>
#include<map>
#include<vector>
#include <algorithm>
#include<iconv.h>
#include "../ysip.hpp"
#include "../db/mmap.hpp"

#define MAX_RECORD_LENGTH 1024
#define MAX_COUNTRY_LENGTH  128
#define MAX_REGION_LENGTH 256
#define IP_LENGTH 4
#define OFFSET_LENGTH 3
#define INDEX_INFO_LENGTH 7
#define MODE_LENGTH 1

#define map_si(a,b) map<string, int>::value_type((a),(b))
#define map_ss(a,b) map<string, string>::value_type((a),(b))

using namespace std;
const static char * data_file = "/home/anders/dev/cnlion/data/ip/qqwry.dat";
const static char * out_file = "/home/anders/dev/cnlion/data/ip/ipdata.utf8";

typedef __uint32_t uint;
typedef unsigned char uchar;
typedef struct IP_DATA_HEADER {
	int from;
	int to;
} IP_DATA_HEADER;

typedef struct IP_DATA_INFO {
	uint minip;
	uint maxip;
	char * country;
	char * region;
	const char * mode;
} IP_DATA_INFO;


map<string, int> region_map;
vector<string> all_country;
map<string, int> region_match_begin;
map<string, string> region_match_replace;

char * get_string_by_offset(int fd, int offset) {
	char p[MAX_REGION_LENGTH];
	int len = pread(fd, p, MAX_REGION_LENGTH, offset);
	if(len > 0) {
		return strdup(trim(p));	
	}
	return NULL;
}

char get_mode_by_offset(int fd, int offset) {
	char mode = 0;
	int len = pread(fd, &mode, sizeof(char), offset);
	if(len > 0) {
		return mode;
	} 
	return 0;
}

uint get_long_by_offset(int fd, int size, int offset) {
	uint num = 0;
	int len = pread(fd, & num, size, offset);
	if(len > 0) {
		return num;
	}
	return 0;
}


char * get_region_by_offset(int fd, int offset) {
	char mode = get_mode_by_offset(fd, offset);
	if(mode == 0x02) {
		return get_string_by_offset(fd, get_long_by_offset(fd, OFFSET_LENGTH, offset + 1));
	}
	return get_string_by_offset(fd, offset);
}

bool get_match_replace() {
	if(region_match_replace.empty()) {
		region_match_replace.insert(map_ss("巢洲市","巢湖市"));
		region_match_replace.insert(map_ss("沙市","沙市区"));
		region_match_replace.insert(map_ss("恩施洲","恩施"));
		region_match_replace.insert(map_ss("增城市","增城区"));
		region_match_replace.insert(map_ss("本溪县","本溪市"));
		region_match_replace.insert(map_ss("加格达奇区","大兴安岭"));
		region_match_replace.insert(map_ss("红岗信息超市","大庆市"));
		region_match_replace.insert(map_ss("红岗信息超市","大庆市"));
		region_match_replace.insert(map_ss("喀左","喀喇沁左翼蒙古族自治县"));
		region_match_replace.insert(map_ss("虹桥区","红桥区"));
		region_match_replace.insert(map_ss("海拉尔市","海拉尔区"));
		region_match_replace.insert(map_ss("宣州市","宣州区"));
		region_match_replace.insert(map_ss("永康","永康市"));
		region_match_replace.insert(map_ss("红河洲蒙自","蒙自县"));
		region_match_replace.insert(map_ss("台北市","台北"));
		region_match_replace.insert(map_ss("河南商商丘市","商丘市"));
		region_match_replace.insert(map_ss("黔江区","黔江开发区"));
		region_match_replace.insert(map_ss("北碚区彩虹桥","北碚区"));
		region_match_replace.insert(map_ss("淮阴市","淮安市"));
		region_match_replace.insert(map_ss("合水市","合水县"));
		region_match_replace.insert(map_ss("甘肃天水市","天水市"));
		region_match_replace.insert(map_ss("内蒙包头市","包头市"));
		region_match_replace.insert(map_ss("集宁市","集宁区"));
		region_match_replace.insert(map_ss("湖北省天门市文化市场","天门市"));
		region_match_replace.insert(map_ss("古乌海市","乌海市"));
		region_match_replace.insert(map_ss("临海","临海市"));
		region_match_replace.insert(map_ss("平潭市","平潭县"));
		region_match_replace.insert(map_ss("平潭市","平潭县"));
		region_match_replace.insert(map_ss("山东省滨洲市","滨州市"));
		region_match_replace.insert(map_ss("山东省郓城市","郓城县"));
		region_match_replace.insert(map_ss("渝北工业园区","渝北区"));
		region_match_replace.insert(map_ss("渝北工业园区","渝北区"));
		region_match_replace.insert(map_ss("文山市","文山县"));
		region_match_replace.insert(map_ss("鄱阳","波阳县"));
		region_match_replace.insert(map_ss("穆凌市","穆棱市"));
		region_match_replace.insert(map_ss("吐鲁番地区","吐鲁番市"));
		region_match_replace.insert(map_ss("哈密地区","哈密市"));
		region_match_replace.insert(map_ss("喀什地区","喀什市"));
		region_match_replace.insert(map_ss("阿克苏地区","阿克苏市"));
		region_match_replace.insert(map_ss("塔城地区","塔城市"));
	}
	return true;
}


bool get_match_begin() {
	if(region_match_begin.empty()) {
		region_match_begin.insert(map_si("北京",99));
		region_match_begin.insert(map_si("北方工业大学",99));
		region_match_begin.insert(map_si("清华大学",99));
		region_match_begin.insert(map_si("北京大学",99));
		region_match_begin.insert(map_si("首都",99));
		region_match_begin.insert(map_si("上海",2076));
		region_match_begin.insert(map_si("中国人民大学",99));
		region_match_begin.insert(map_si("华中农业大学",1079));
		region_match_begin.insert(map_si("中央财经大学",99));
		region_match_begin.insert(map_si("华南理工大学",287));
		region_match_begin.insert(map_si("南开大学",2260));
		region_match_begin.insert(map_si("吉林大学",1246));
		region_match_begin.insert(map_si("哈尔滨",945));
		region_match_begin.insert(map_si("暨南大学",287));
		region_match_begin.insert(map_si("朝阳区",99));
		region_match_begin.insert(map_si("浙江大学",2595));
		region_match_begin.insert(map_si("海淀区",99));
		region_match_begin.insert(map_si("南开大学",2260));
		region_match_begin.insert(map_si("西安",2031));
		region_match_begin.insert(map_si("长江大学",1058));
		region_match_begin.insert(map_si("中国电信天上人间信息超市",99));
		region_match_begin.insert(map_si("乌兰察布盟",4091));
		region_match_begin.insert(map_si("呼伦贝尔盟",1599));
		region_match_begin.insert(map_si("湖南省永州市江华县", 1204));
		region_match_begin.insert(map_si("湖南省邵阳市城步县", 1171));
		region_match_begin.insert(map_si("云南省德宏洲瑞丽市", 2484));
		region_match_begin.insert(map_si("河池市都安县", 441));
		region_match_begin.insert(map_si("南宁市秀灵路", 474));
		region_match_begin.insert(map_si("四川省成都市陴县", 4452));
		region_match_begin.insert(map_si("四川省自贡市", 2248));
		region_match_begin.insert(map_si("山东省枣庄市", 1842));
		region_match_begin.insert(map_si("黑龙江省伊春市伊春区", 1020));
		region_match_begin.insert(map_si("河南省骡河市", 921));
		region_match_begin.insert(map_si("贵港市贵城镇", 435));
		region_match_begin.insert(map_si("大连理工大学", 1493));
		region_match_begin.insert(map_si("武汉大学", 1079));
		region_match_begin.insert(map_si("华中科技大学", 1079));
		region_match_begin.insert(map_si("西华大学", 2098));
		region_match_begin.insert(map_si("成都信息工程学院", 2098));
		region_match_begin.insert(map_si("四川师范大学", 2098));
		region_match_begin.insert(map_si("江西农业大学", 1435));
		region_match_begin.insert(map_si("华东交通大学", 1435));
		region_match_begin.insert(map_si("四川大学", 2098));
		region_match_begin.insert(map_si("成都工业学院", 2098));
		region_match_begin.insert(map_si("福州大学", 106));
		region_match_begin.insert(map_si("福建师范大学", 106));
		region_match_begin.insert(map_si("东南大学", 1309));
		region_match_begin.insert(map_si("集美大学", 168));
		region_match_begin.insert(map_si("厦门大学", 168));
		region_match_begin.insert(map_si("江苏省盐城师范学院", 1358));
		region_match_begin.insert(map_si("对外经济贸易大学", 99));
		region_match_begin.insert(map_si("重庆大学", 2696));
		region_match_begin.insert(map_si("长春工业大学", 1246));
		region_match_begin.insert(map_si("湖南师范大学", 1126));
		region_match_begin.insert(map_si("郑州大学", 892));
		region_match_begin.insert(map_si("同济大学", 2076));
		region_match_begin.insert(map_si("四川省乐山市", 2160));
		region_match_begin.insert(map_si("甘肃省兰州城关区", 218));
		region_match_begin.insert(map_si("兰州市商学院", 218));
		region_match_begin.insert(map_si("山东省青岛市市北区",3565));
		region_match_begin.insert(map_si("四川高县",2231));

 region_match_begin.insert(map_si("东北农业大学",1));
 region_match_begin.insert(map_si("东北大学",1));
 region_match_begin.insert(map_si("东北林业大学",1));
 region_match_begin.insert(map_si("东华大学",1));
 region_match_begin.insert(map_si("东华理工大学",1));
 region_match_begin.insert(map_si("中北大学",1));
 region_match_begin.insert(map_si("中南大学",1));
 region_match_begin.insert(map_si("中南大学岳麓山校区",1));
 region_match_begin.insert(map_si("中南财经政法大学",1));
 region_match_begin.insert(map_si("中国农业大学",1));
 region_match_begin.insert(map_si("中国农业大学东校区",1));
 region_match_begin.insert(map_si("中国农业大学西区员工绿苑",1));
 region_match_begin.insert(map_si("中山大学",1));
 region_match_begin.insert(map_si("云南大学",1));
 region_match_begin.insert(map_si("佳木斯大学",1));
 region_match_begin.insert(map_si("兰州大学",1));
 region_match_begin.insert(map_si("兰州大学榆中校区",1));
 region_match_begin.insert(map_si("农业大学",1));
 region_match_begin.insert(map_si("华东师范大学",1));
 region_match_begin.insert(map_si("华东理工大学",1));
 region_match_begin.insert(map_si("华南农业大学",1));
 region_match_begin.insert(map_si("华南农业大学华山区",1));
 region_match_begin.insert(map_si("南京化工大学",1));
 region_match_begin.insert(map_si("南京大学",1));
 region_match_begin.insert(map_si("南京大学",1));
 region_match_begin.insert(map_si("南京大学鼓楼校区",1));
 region_match_begin.insert(map_si("南京工业大学",1));
 region_match_begin.insert(map_si("南京理工大学",1));
 region_match_begin.insert(map_si("南昌大学",1));
 region_match_begin.insert(map_si("南通大学",1));
 region_match_begin.insert(map_si("台湾大学",1));
 region_match_begin.insert(map_si("合肥工业大学",1));
 region_match_begin.insert(map_si("四川农业大学",1));
 region_match_begin.insert(map_si("四川西南科技大学",1));
 region_match_begin.insert(map_si("大庆职工大学",1));
 region_match_begin.insert(map_si("太原科技大学",1));
 region_match_begin.insert(map_si("宁波大学",1));
 region_match_begin.insert(map_si("安徽农业大学",1));
 region_match_begin.insert(map_si("安徽大学",1));
 region_match_begin.insert(map_si("安徽财经大学",1));
 region_match_begin.insert(map_si("山东大学",1));
 region_match_begin.insert(map_si("成都中医药大学",1));
 region_match_begin.insert(map_si("成都大学",1));
 region_match_begin.insert(map_si("成都理工大学",1));
 region_match_begin.insert(map_si("新西兰基督城坎特伯雷大学",1));
 region_match_begin.insert(map_si("武汉科技大学",1));
 region_match_begin.insert(map_si("武汉科技大学南校区",1));
 region_match_begin.insert(map_si("江西师范大学",1));
 region_match_begin.insert(map_si("江西财经大学",1));
 region_match_begin.insert(map_si("河北大学",1));
 region_match_begin.insert(map_si("浙江工业大学",1));
 region_match_begin.insert(map_si("浙江广播电视大学",1));
 region_match_begin.insert(map_si("湖北大学",1));
 region_match_begin.insert(map_si("湖南农业大学",1));
 region_match_begin.insert(map_si("湖南科技大学",1));
 region_match_begin.insert(map_si("西北工业大学",1));
 region_match_begin.insert(map_si("贵州工业大学",1));
 region_match_begin.insert(map_si("资阳广播电视大学",1));
 region_match_begin.insert(map_si("长沙理工大学",1));
 region_match_begin.insert(map_si("长沙理工大学",1));
 region_match_begin.insert(map_si("青岛大学",1));
 region_match_begin.insert(map_si("青岛科技大学",1));
 region_match_begin.insert(map_si("青海大学",1));
 region_match_begin.insert(map_si("黄河科技大学",1));
 region_match_begin.insert(map_si("黑龙江大学",1));
 region_match_begin.insert(map_si("黑龙江广播电视大学",1));
 region_match_begin.insert(map_si("雅虎中国", 3122));
 region_match_begin.insert(map_si("天津市河平区", 3153));


		

 region_match_begin.insert(map_si("中国农业科学院",1));
 region_match_begin.insert(map_si("华北科技学院",1));
 region_match_begin.insert(map_si("南昌工程学院",1));
 region_match_begin.insert(map_si("南昌理工学院",1));
 region_match_begin.insert(map_si("四川理工学院",1));
 region_match_begin.insert(map_si("四川行政学院",1));
 region_match_begin.insert(map_si("安徽建筑工业学院",1));
 region_match_begin.insert(map_si("安徽电子信息技术学院",1));
 region_match_begin.insert(map_si("汉中理工学院",1));
 region_match_begin.insert(map_si("江西九江学院",1));
 region_match_begin.insert(map_si("江西财经学院九江分院",1));
 region_match_begin.insert(map_si("泉州师范学院",1));
 region_match_begin.insert(map_si("浙江科技学院",1));
 region_match_begin.insert(map_si("浙江轻纺学院",1));
 region_match_begin.insert(map_si("湖北第二师范学院",1));
 region_match_begin.insert(map_si("湖南商学院",1));
 region_match_begin.insert(map_si("潍坊学院",1));
 region_match_begin.insert(map_si("福建工程学院",1));
 region_match_begin.insert(map_si("重庆信息工程专修学院",1));
 region_match_begin.insert(map_si("重庆工学院",1));
 region_match_begin.insert(map_si("长春职业技术学院",1));
 region_match_begin.insert(map_si("长沙交通学院",1));
 region_match_begin.insert(map_si("长沙电力职业技术学院",1));
 region_match_begin.insert(map_si("青海建筑职业技术学院",1));
 region_match_begin.insert(map_si("黑龙江东方学院",1));
 region_match_begin.insert(map_si("齐齐哈尔医学院",1));
 region_match_begin.insert(map_si("莫旗",1601)); //莫力达瓦
 region_match_begin.insert(map_si("中国测绘院",99)); //
 region_match_begin.insert(map_si("重庆市万州区",3176)); //
 //国外
 region_match_begin.insert(map_si("美国",100000));
 region_match_begin.insert(map_si("德国",100000));
 region_match_begin.insert(map_si("挪威",100000));
 region_match_begin.insert(map_si("沙特阿拉伯",100000));
 region_match_begin.insert(map_si("西班牙",100000));
 region_match_begin.insert(map_si("IANA",100000));
 region_match_begin.insert(map_si("亚太地区",100000));
 region_match_begin.insert(map_si("法属玻利尼西亚",100000));
 region_match_begin.insert(map_si("密克罗尼西亚",100000));
 region_match_begin.insert(map_si("意大利",100000));

	}
	return true;
}
bool get_region_map() {
	//int len = 0, i = 0;
	if(region_map.empty()) {
	//region_map.insert(map<string, int>::value_type("安徽",47));
	//region_map.insert(map<string, int>::value_type("北京",99));
	//region_map.insert(map<string, int>::value_type("福建",106));
	//region_map.insert(map<string, int>::value_type("甘肃",218));
	//region_map.insert(map<string, int>::value_type("广东",287));
	region_map.insert(map<string, int>::value_type("广西",474));
	//region_map.insert(map<string, int>::value_type("贵州",513));
	//region_map.insert(map<string, int>::value_type("海南",601));
	//region_map.insert(map<string, int>::value_type("河北",728));
	//region_map.insert(map<string, int>::value_type("河南",892));
	//region_map.insert(map<string, int>::value_type("黑龙江",945));
	//region_map.insert(map<string, int>::value_type("湖北",1079));
	//region_map.insert(map<string, int>::value_type("湖南",1126));
	//region_map.insert(map<string, int>::value_type("吉林",1246));
	//region_map.insert(map<string, int>::value_type("江苏",1309));
	//region_map.insert(map<string, int>::value_type("江西",1435));
	//region_map.insert(map<string, int>::value_type("辽宁",1534));
	region_map.insert(map<string, int>::value_type("内蒙古",1587));
	region_map.insert(map<string, int>::value_type("宁夏",1672));
	//region_map.insert(map<string, int>::value_type("青海",1713));
	//region_map.insert(map<string, int>::value_type("山东",1760));
	//region_map.insert(map<string, int>::value_type("山西",1933));
	//region_map.insert(map<string, int>::value_type("陕西",2031));
	//region_map.insert(map<string, int>::value_type("上海",2076));
	//region_map.insert(map<string, int>::value_type("四川",2098));
	//region_map.insert(map<string, int>::value_type("天津",2260));
	region_map.insert(map<string, int>::value_type("西藏",2286));
	region_map.insert(map<string, int>::value_type("新疆",2421));
	//region_map.insert(map<string, int>::value_type("云南",2509));
	//region_map.insert(map<string, int>::value_type("浙江",2595));
	//region_map.insert(map<string, int>::value_type("重庆",2696));
	//region_map.insert(map<string, int>::value_type("香港",4419));
	//region_map.insert(map<string, int>::value_type("澳门",4440));
	//region_map.insert(map<string, int>::value_type("台湾",4458));
	region_map.insert(map_si("首都",99));
	region_map.insert(map_si("中国",99));
	}
	return true;
}


bool get_all_country() {
	if(all_country.empty()) {
		all_country.push_back("AFRINIC");
		all_country.push_back("APNIC");
		all_country.push_back("ARIN");
		all_country.push_back("CNNIC");
		all_country.push_back("CZ88.NET");
		all_country.push_back("Equinix");
		all_country.push_back("Firebrand");
		all_country.push_back("Heartlandis");
		all_country.push_back("IANA");
		all_country.push_back("LACNIC");
		all_country.push_back("Microsoft");
		all_country.push_back("REPUBLIC");
		all_country.push_back("RIPE");
		all_country.push_back("Teleglobe");
		all_country.push_back("Yahoo");
		all_country.push_back("不丹");
		all_country.push_back("东帝汶");
		all_country.push_back("东萨摩亚");
		all_country.push_back("中非");
		all_country.push_back("丹麦");
		all_country.push_back("乌克兰");
		all_country.push_back("乌干达");
		all_country.push_back("乌拉圭");
		all_country.push_back("乌苏里");
		all_country.push_back("乍得");
		all_country.push_back("乔治亚");
		all_country.push_back("也门");
		all_country.push_back("亚美尼亚");
		all_country.push_back("以色列");
		all_country.push_back("伊拉克");
		all_country.push_back("伊朗");
		all_country.push_back("伯利兹");
		all_country.push_back("佛得角");
		all_country.push_back("俄罗斯");
		all_country.push_back("俄罗斯科");
		all_country.push_back("保加利亚");
		all_country.push_back("克罗地亚");
		all_country.push_back("关岛");
		all_country.push_back("冈比亚");
		all_country.push_back("冰岛");
		all_country.push_back("刚果");
		all_country.push_back("利比亚");
		all_country.push_back("利比里亚");
		all_country.push_back("加拿大");
		all_country.push_back("加纳");
		all_country.push_back("加蓬");
		all_country.push_back("匈牙利");
		all_country.push_back("南非");
		all_country.push_back("博茨瓦纳");
		all_country.push_back("卡塔尔");
		all_country.push_back("卢旺达");
		all_country.push_back("卢森堡");
		all_country.push_back("印尼");
		all_country.push_back("印度");
		all_country.push_back("印度尼西亚");
		all_country.push_back("危地马拉");
		all_country.push_back("厄瓜多尔");
		all_country.push_back("叙利亚");
		all_country.push_back("古巴");
		all_country.push_back("吉布提");
		all_country.push_back("哥伦比亚");
		all_country.push_back("喀麦隆");
		all_country.push_back("图瓦卢");
		all_country.push_back("土尔其");
		all_country.push_back("土耳其");
		all_country.push_back("圣地亚哥");
		all_country.push_back("圣马力诺");
		all_country.push_back("圭亚那");
		all_country.push_back("坦桑尼亚");
		all_country.push_back("埃及");
		all_country.push_back("基里巴斯");
		all_country.push_back("塞内加尔");
		all_country.push_back("塞尔维亚");
		all_country.push_back("塞拉利昂");
		all_country.push_back("塞浦路斯");
		all_country.push_back("塞舌尔");
		all_country.push_back("塞黑");
		all_country.push_back("墨西哥");
		all_country.push_back("多哥");
		all_country.push_back("多米尼加");
		all_country.push_back("奥地利");
		all_country.push_back("奥大利亚");
		all_country.push_back("委内瑞拉");
		all_country.push_back("孟加拉");
		all_country.push_back("孟加拉国");
		all_country.push_back("安哥拉");
		all_country.push_back("安地瓜");
		all_country.push_back("安提瓜岛");
		all_country.push_back("安道尔");
		all_country.push_back("尼加拉瓜");
		all_country.push_back("尼日利亚");
		all_country.push_back("尼日尔");
		all_country.push_back("尼泊尔");
		all_country.push_back("局域网");
		all_country.push_back("巴勒斯坦");
		all_country.push_back("巴哈马");
		all_country.push_back("巴基斯坦");
		all_country.push_back("巴巴多斯");
		all_country.push_back("巴拉圭");
		all_country.push_back("巴拿马");
		all_country.push_back("巴林");
		all_country.push_back("巴西");
		all_country.push_back("布隆迪");
		all_country.push_back("希腊");
		all_country.push_back("帕劳");
		all_country.push_back("开曼群岛");
		all_country.push_back("德国");
		all_country.push_back("德国");
		all_country.push_back("瑞典");
		all_country.push_back("意大利");
		all_country.push_back("拉托维亚");
		all_country.push_back("拉脱维亚");
		all_country.push_back("挪威");
		all_country.push_back("挪威");
		all_country.push_back("捷克");
		all_country.push_back("摩尔多瓦");
		all_country.push_back("摩洛哥");
		all_country.push_back("摩纳哥");
		all_country.push_back("文莱");
		all_country.push_back("斐济");
		all_country.push_back("斯威士兰");
		all_country.push_back("斯洛伐克");
		all_country.push_back("斯里兰卡");
		all_country.push_back("新加坡");
		all_country.push_back("新西兰");
		all_country.push_back("日本");
		all_country.push_back("智利");
		all_country.push_back("未分配");
		all_country.push_back("本机地址");
		all_country.push_back("柬埔寨");
		all_country.push_back("格鲁吉亚");
		all_country.push_back("梵蒂冈");
		all_country.push_back("欧洲");
		all_country.push_back("比利时");
		all_country.push_back("毛里求斯");
		all_country.push_back("汤加");
		all_country.push_back("法国");
		all_country.push_back("法罗群岛");
		all_country.push_back("波兰");
		all_country.push_back("波多黎各");
		all_country.push_back("波斯尼亚");
		all_country.push_back("波黑");
		all_country.push_back("泰国");
		all_country.push_back("津巴布韦");
		all_country.push_back("洪都拉斯");
		all_country.push_back("浪点网吧");
		all_country.push_back("海地");
		all_country.push_back("澳大利亚");
		all_country.push_back("澳洲");
		all_country.push_back("爱尔兰");
		all_country.push_back("爱沙尼亚");
		all_country.push_back("牙买加");
		all_country.push_back("玻利维亚");
		all_country.push_back("瑙鲁");
		all_country.push_back("瑞典");
		all_country.push_back("瑞士");
		all_country.push_back("瑞士/英国");
		all_country.push_back("瑞士/列支敦");
		all_country.push_back("荷属安的列斯");
		all_country.push_back("英属印度洋");
		all_country.push_back("雅虎亚洲网络");
		all_country.push_back("北马里亚纳群岛");
		all_country.push_back("瓦努阿图");
		all_country.push_back("留尼汪岛");
		all_country.push_back("白俄罗斯");
		all_country.push_back("百慕大");
		all_country.push_back("直布罗陀");
		all_country.push_back("科威特");
		all_country.push_back("科摩罗");
		all_country.push_back("科特迪瓦");
		all_country.push_back("秘鲁");
		all_country.push_back("突尼斯");
		all_country.push_back("立陶宛");
		all_country.push_back("第二炮兵");
		all_country.push_back("索马里");
		all_country.push_back("约旦");
		all_country.push_back("纳米比亚");
		all_country.push_back("纽埃");
		all_country.push_back("维多利亚");
		all_country.push_back("缅甸");
		all_country.push_back("罗尼西亚");
		all_country.push_back("罗马利亚");
		all_country.push_back("罗马尼亚");
		all_country.push_back("老挝");
		all_country.push_back("联合国");
		all_country.push_back("聚友网络");
		all_country.push_back("肯尼亚");
		all_country.push_back("芬兰");
		all_country.push_back("苏丹");
		all_country.push_back("苏格兰");
		all_country.push_back("苏里南");
		all_country.push_back("英国");
		all_country.push_back("美国");
		all_country.push_back("英格兰");
		all_country.push_back("荷兰");
		all_country.push_back("荷兰格");
		all_country.push_back("莫桑比克");
		all_country.push_back("菲律宾");
		all_country.push_back("萨尔瓦多");
		all_country.push_back("葡萄牙");
		all_country.push_back("蒙古");
		all_country.push_back("西班牙");
		all_country.push_back("西萨摩亚");
		all_country.push_back("诺福克岛");
		all_country.push_back("象牙海岸");
		all_country.push_back("贝宁");
		all_country.push_back("赞比亚");
		all_country.push_back("越南");
		all_country.push_back("长城宽带");
		all_country.push_back("阿塞拜疆");
		all_country.push_back("阿富汗");
		all_country.push_back("阿曼");
		all_country.push_back("阿根廷");
		all_country.push_back("阿联酋");
		all_country.push_back("阿鲁巴");
		all_country.push_back("雅加达");
		all_country.push_back("韩国");
		all_country.push_back("马其顿");
		all_country.push_back("马尔代夫");
		all_country.push_back("马来西亚");
		all_country.push_back("马耳他");
		all_country.push_back("马里");
		all_country.push_back("黎巴嫩");
		all_country.push_back("所罗门群岛");
		all_country.push_back("托克劳群岛");
		all_country.push_back("捷克共和国");
		all_country.push_back("阿尔及利亚");
		all_country.push_back("莱索托");
		all_country.push_back("布基纳法索");
		all_country.push_back("马达加斯加");
		all_country.push_back("马拉维");
		all_country.push_back("马里共和国");
		all_country.push_back("赤道几内亚");
		all_country.push_back("刚果民主共和国");
		all_country.push_back("中非共和国");
		all_country.push_back("布基纳法索");
		all_country.push_back("哈萨克斯坦");
		all_country.push_back("塔吉克斯坦");
		all_country.push_back("塞尔维亚和黑山");
		all_country.push_back("斯洛文尼亚");
		all_country.push_back("列支敦士登");
		all_country.push_back("安提瓜和巴布");
		all_country.push_back("埃塞俄比亚");
		all_country.push_back("吉尔吉斯斯坦");
		all_country.push_back("新喀里多尼亚");
		all_country.push_back("阿尔巴尼亚");
		all_country.push_back("REPUBLIC OF KOREA");
		all_country.push_back("特立尼达和多巴哥");
		all_country.push_back("BITS国际公司欧洲IP分布");
		all_country.push_back("瓜德罗普岛");
		all_country.push_back("乌兹别克斯坦");
		all_country.push_back("毛里塔尼亚");
		all_country.push_back("毛里塔尼亚");
		all_country.push_back("朝鲜");
		all_country.push_back("几内亚比绍");
		all_country.push_back("土库曼斯坦");
		all_country.push_back("格陵兰群岛");
		all_country.push_back("哥斯达黎加");
		all_country.push_back("Equinix亚太公司");
		all_country.push_back("新喀里多尼亚岛");
		all_country.push_back("Sprint亚太地区网络");
		all_country.push_back("Concert亚太区网络");

		all_country.push_back("AsiaNetcom公司");
		all_country.push_back("Intelsat公司");
		all_country.push_back("厄立特里亚");
		all_country.push_back("圣多美和普林");
		all_country.push_back("埃及/约旦/黎巴嫩");
		all_country.push_back("塞浦路斯/德");
		all_country.push_back("多米尼加共和国");
		all_country.push_back("多米泥加共和国");
		all_country.push_back("奥地利/比利时");
		all_country.push_back("巴布亚新几内亚");
		all_country.push_back("文莱达鲁萨兰");
		all_country.push_back("法属波利尼西亚");
		all_country.push_back("法属玻利尼西");
		all_country.push_back("法属玻里尼西");
		all_country.push_back("特克斯和凯科斯群岛");
		all_country.push_back("特立尼和多巴哥");
		all_country.push_back("瓦利斯和富图纳群岛");
		all_country.push_back("百慕大群岛");
		all_country.push_back("罗马尼亚布");
		all_country.push_back("美属萨摩亚");
		all_country.push_back("萨摩亚");
		all_country.push_back("阿拉伯联合酋长国");
		all_country.push_back("雅虎");
		all_country.push_back("非公网地址");
		all_country.push_back("韩国，");
		all_country.push_back("马提尼克岛");
		all_country.push_back("马绍尔群岛");
		all_country.push_back("黑山共和国");

		//other
		all_country.push_back("纯真网络");
		all_country.push_back("未知物理地址");


	}
	return true;
}
void out_ip_data_info(FILE *fp, IP_DATA_INFO * ip_data_info) {
	char *out, *p;
	size_t in_len, out_len , ret;
	int id = -1;
	int len = 0;
	int mode = 0;
	char country[1024];
	char region[1024];

	REGION_INFO region_info;
	iconv_t cd = iconv_open("UTF-8//IGNORE", "GB18030");
	if(cd == (iconv_t)-1) {
		cerr << "Init iconv error!" << endl;
		return;
	}

	out_len = 1023;
	out = country;
	p = ip_data_info->country;
	in_len = strlen(p);
	memset(out, 0, 1024);
	ret = iconv(cd, &p, &in_len, &out, &out_len);
	if(ret == -1) {
		cerr << ip_data_info->minip << endl;
		cerr << "Convert failed:" << strerror(errno) << endl;
	}

	out = region;
	p = ip_data_info->region;
	in_len = strlen(p);
	memset(out, 0, 1024);
	ret = iconv(cd, &p, &in_len, &out, &out_len);

	if(ret == -1) {
		cerr << ip_data_info->minip << endl;
		cerr << "Convert failed:" << strerror(errno) << endl;
	}


	fprintf(fp, "%u %u %d %s %s\n", ip_data_info->minip, 
				ip_data_info->maxip, 
				0,
				country,
				region);
	if(cd != NULL) {
		iconv_close(cd);
	}

	/*

	fprintf(fp, "%u %u %d %s %s\n", ip_data_info->minip, 
				ip_data_info->maxip, 
				0,
				ip_data_info->country,
				ip_data_info->region);
	*/


	/*
	char *out, *p;
	size_t in_len, out_len , ret;
	int id = -1;
	int len = 0;
	int mode = 0;
	char country[1024];
	char region[1024];
	REGION_INFO region_info;
	iconv_t cd = iconv_open("UTF-8//IGNORE", "GB18030");
	if(cd == (iconv_t)-1) {
		cerr << "Init iconv error!" << endl;
		return;
	}

	out_len = 1023;
	out = country;
	p = ip_data_info->country;
	in_len = strlen(p);
	memset(out, 0, 1024);
	ret = iconv(cd, &p, &in_len, &out, &out_len);
	if(ret == -1) {
		cerr << "Convert failed:" << strerror(errno) << endl;
	}

	out = region;
	p = ip_data_info->region;
	in_len = strlen(p);
	memset(out, 0, 1024);
	ret = iconv(cd, &p, &in_len, &out, &out_len);

	if(ret == -1) {
		cerr << "Convert failed:" << strerror(errno) << endl;
	}


	string region_name = country;	
	string area = "";
	vector<string>::iterator v_iter = find(all_country.begin(), all_country.end(), (string)country);
	if(v_iter != all_country.end()) {
		id = 100000;
	}

	map<string,int>::iterator m_iter;
	for(m_iter = region_match_begin.begin(); m_iter != region_match_begin.end(); m_iter++) {
		if(region_name.find(m_iter->first) != string::npos) {
			id = m_iter->second;
			break;
		}
	}
	
	if(id == -1 && get_region_info_by_name(country, &region_info)) {
		id = region_info.id;	
	}

	map <string,string>::iterator mss_iter;
	if( id == -1) {
		map <string, int>::iterator iter;
		try {
			for(iter = region_map.begin(); iter != region_map.end(); iter ++) {
				ret = region_name.find(iter->first);
				if(ret == 0) {
					region_name = region_name.substr(iter->first.length());
					if(region_name.length() == 0) {
						mode = 0;
						id = iter->second;	
					}
					break;
				}
			}
		} catch (exception &e) {
			cerr << "exception occur: " << e.what() << endl ;
		}
		
		if(id == -1) {
			size_t idx_xian = region_name.rfind("县");
			size_t idx_shi = region_name.rfind("市");
			size_t idx_shi_2 = region_name.find("市");
			size_t idx_sheng = region_name.rfind("省");
			size_t idx_zhou = region_name.rfind("州");
			size_t idx_meng = region_name.rfind("盟");
			size_t idx_diqu = region_name.rfind("地区");
			size_t idx_qu = region_name.rfind("区");
			try {
				// if xx 市 xx 县 
				//cerr << " regionname is " << region_name << endl;
				//cerr << " idx_xian " << idx_xian << " idx_shi " << idx_shi << " idx_sheng " << idx_sheng << endl;
				if(idx_shi != string::npos && idx_xian != string::npos && idx_xian > idx_shi) {
					area = region_name.substr(idx_shi + strlen("市"), idx_xian - idx_shi);
					mode = 1;

					// if xx 省 xx 州　xx 县
				} else if(idx_sheng != string::npos && idx_zhou != string::npos && idx_xian != string::npos && idx_xian > idx_zhou && idx_zhou > idx_sheng && idx_zhou - idx_xian >= strlen("市") * 2) {
					area = region_name.substr(idx_zhou + strlen("省"), idx_xian - idx_zhou);	
					mode = 21;
					// if xx 省 xx 县
				} else if(idx_sheng != string::npos && idx_xian != string::npos && idx_xian > idx_sheng) {
					area = region_name.substr(idx_sheng + strlen("省"), idx_xian - idx_sheng);	
					mode = 2;
					// if xx 市 xx 市
				} else if(idx_shi != string::npos && idx_shi_2 != string::npos && idx_shi != idx_shi_2 && idx_shi_2 - idx_shi > strlen("市市")) {
					area = region_name.substr(idx_shi_2 + strlen("市"),idx_shi - idx_shi_2);
					mode = 3;
					// if xx 省 xx 州 xx 市
				} else if(idx_sheng != string::npos && idx_shi != string::npos && idx_zhou != string::npos && idx_shi > idx_zhou && idx_zhou > idx_sheng && idx_shi - idx_zhou >= strlen("州") * 2) {
					area = region_name.substr(idx_zhou + strlen("州"),idx_shi - idx_zhou);
					mode = 20;
					// if xx 省 xx 市
				} else if(idx_sheng != string::npos && idx_shi != string::npos && idx_shi > idx_sheng) {
					area = region_name.substr(idx_sheng + strlen("省"), idx_shi - idx_sheng);	
					mode = 4;
					// if xx 县 
				} else if(idx_xian != string::npos) {
					area = region_name.substr(idx_xian + strlen("县"));
					mode = 5;
					// if xx 市 xx 区
				} else if (idx_shi != string::npos && idx_qu != string::npos && idx_qu > idx_shi) {
					area = region_name.substr(idx_shi + strlen("市"), idx_qu - idx_shi);
					mode = 51;
					// if xx 市
				} else if(idx_shi != string::npos && region_name.length() != idx_shi + strlen("市")) {
					area = region_name.substr(idx_shi + strlen("市"));
					mode = 6;
					// if xx省xx州
				} else if(idx_zhou != string::npos && idx_sheng != string::npos && idx_zhou > idx_sheng) {
					area = region_name.substr(idx_sheng + strlen("省"), idx_zhou - idx_sheng);	
					area = area.substr(0, idx_zhou - idx_sheng - strlen("州"));
					mode = 7;
					// if xx省xx地区
				} else if(idx_sheng != string::npos && idx_diqu != string::npos && idx_diqu > idx_sheng) {
					area = region_name.substr(idx_sheng + strlen("省"), idx_diqu - idx_sheng - 3);	
					mode = 71; 
					// if only xx 省 xx
				} else if(idx_sheng != string::npos && idx_sheng + strlen("省") < region_name.length()) {
					area = region_name.substr(idx_sheng + strlen("省"));
					mode = 8;
//				} else if(idx_zhou != string::npos && 
				} else if(idx_zhou != string::npos && idx_zhou >= strlen("州") * 2) {
					area = region_name.substr(0, idx_zhou);
					mode = 9;
		//		} else if(idx_meng != string::npos) {
		//			area = region_name.substr(0, idx_meng);
		//			mode = 10;
				} else {
					if(idx_sheng + strlen("省") == region_name.length()) {
						area = region_name.substr(0, idx_sheng);
						mode = 23;
					} else {
						area = region_name;
						mode = 11;
					}
				}

		for(mss_iter = region_match_replace.begin(); mss_iter != region_match_replace.end(); mss_iter++) {
			if(mss_iter->first == area)
			{
				area = mss_iter->second;
			}
		}

				if(get_region_info_by_name(area, &region_info)) {
					id = region_info.id;	
				}
				if(id == -1) {
					if(area.rfind("县") != string::npos) {
						area = area.substr(0, area.length() - strlen("县"));
					}
					if(get_region_info_by_name(area, & region_info)) {
						id = region_info.id;
					}
				}
				if(id == -1) {
					if(area.rfind("洲") != string::npos) {
						area = area.replace(area.rfind("洲"),3,"州");
						if(get_region_info_by_name(area, &region_info)) {
							id= region_info.id;
						}
					}
				}
				if(id == -1) {
					cerr << "area is '" << area << "' region is " << region_name <<" mode is " << mode << endl;
				}
			} catch (exception &e) {
				//cerr << e.what() << endl;	
			}
		}
	}

	fprintf(fp, "%u %u %d %s %s\n", ip_data_info->minip, ip_data_info->maxip, id, country, region);

	if(cd != NULL) {
		iconv_close(cd);
	}
*/
}


bool get_ip_data_info(int fd, int offset, IP_DATA_INFO * ip_data_info) {
	int mode_offset;
	char mode = get_mode_by_offset(fd, offset);
	if(mode == 0x01) {
		mode_offset = get_long_by_offset(fd, OFFSET_LENGTH, offset + 1);		
		get_ip_data_info(fd, mode_offset, ip_data_info);
	} else if(mode == 0x02) {
		mode_offset = get_long_by_offset(fd, OFFSET_LENGTH, offset + 1);
		ip_data_info->country = get_string_by_offset(fd, mode_offset);
		offset = offset + 1 + OFFSET_LENGTH;
		mode = get_mode_by_offset(fd, offset);
		if(mode == 0x02) {
			mode_offset = get_long_by_offset(fd, OFFSET_LENGTH, offset + 1);	
			ip_data_info->region = get_string_by_offset(fd, mode_offset);
		} else {
			ip_data_info->region = get_string_by_offset(fd, offset);	
		}
	} else {
		// 读取国家名 
		ip_data_info->country = get_string_by_offset(fd,offset);
		// 读取地区名
		ip_data_info->region = get_region_by_offset(fd, offset + strlen(ip_data_info->country) + 1);
		ip_data_info->mode = "x x";
	}
	return true;
}

int main( int argc, char ** argv) {
	int fd;
	int length, count, i, succ, fail;
	int offset;
	uint minip;
	IP_DATA_HEADER ip_data_header;
	IP_DATA_INFO ip_data_info;
	FILE * fp;
	fd = open(data_file, O_RDONLY);
	fp = fopen(out_file, "w+");

	if(fd < 0) {
		cerr << "Cann't open file " << data_file << " to read!" << endl;
		return 1;
	}

	if(fp == NULL) {
		cerr << "Cann't open file " << out_file << " to write!" << endl;
		close(fd);
		return 1;
	}
	length = read(fd, &ip_data_header, sizeof(IP_DATA_HEADER));
	if(length > 0) {
		cout << "Read header: " << endl;
		cout << " index first offset is : " << ip_data_header.from << endl;
		cout << " index last offset is : " << ip_data_header.to << endl;
	}

	if(!get_instance()) {
		cerr << " get instance of ysip error ! " << endl;
		return 1;
	}

	if(!get_all_country()) {
		cerr << " get all country error!" << endl;
		return 1;
	}
	if(!get_region_map()) {
		cerr << " get region map error!" << endl;
		return 1;
	}

	if(!get_match_begin()) {
		cerr << " get match begin !" << endl;
		return 1;
	}

	if(!get_match_replace()) {
		cerr << " get match replace !" << endl;
		return 1;
	}
	count = (ip_data_header.to - ip_data_header.from) / INDEX_INFO_LENGTH + 1;
	succ = 0;
	fail = 0;
	for( i = 0 ; i < count ; i ++ ) {
		memset(&ip_data_info, 0, sizeof(IP_DATA_INFO));
		minip = get_long_by_offset(fd, IP_LENGTH, ip_data_header.from + i * INDEX_INFO_LENGTH);
		offset = get_long_by_offset(fd, OFFSET_LENGTH, ip_data_header.from + i * INDEX_INFO_LENGTH + IP_LENGTH);
		if(minip >= 0) {
			succ ++;
			ip_data_info.minip = minip;	
			ip_data_info.maxip = get_long_by_offset(fd, IP_LENGTH, offset);
			if(get_ip_data_info(fd, offset + IP_LENGTH,  &ip_data_info)) {
				out_ip_data_info(fp, & ip_data_info);
			}
			if(ip_data_info.region != NULL) {
				free(ip_data_info.region);
			} 
			if(ip_data_info.country != NULL) {
				free(ip_data_info.country);
			}
		} else {
			fail ++;
		}
	}

	cout << "Read ip: " <<endl;
	cout << "	succ " << succ << endl;
	cout << " fail " << fail << endl;           
	close(fd);
	fclose(fp);
	return 0;
}
